/*
 @desc: 首页
 @date: 2021/12/27
 */

#import "ViewController.h"
#import "UserViewController.h"
#import "HomeViewModel.h"
#import "HomeCollectionViewCell.h"

#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) UICollectionView *mCollectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *mFlow;

@property (nonatomic,strong) HomeViewModel * mViewModel;
@property (weak, nonatomic) IBOutlet UIButton *addBt;
@property (weak, nonatomic) IBOutlet UIButton *moreBt;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self configureCollectionView];
    [self configureViewModel];
}

- (IBAction)didClickUserAction:(UIButton *)sender {
    UserViewController * vc = [[UserViewController alloc] init];
    [self showViewController:vc sender:nil];
}

- (IBAction)didClickEditAction:(UIButton *)sender {
    [self openAlbum];
}
/// 打开相册
- (void)openAlbum{
    UIImagePickerController *imagePicker= [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//    if (@available(iOS 15.0, *)) {
//        imagePicker.mediaTypes = @[(NSString *)UTTypeImage];
//    }else {
    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
//    }
    
    imagePicker.allowsEditing = NO;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

//TEST
-(void)testAction {
    UIImage * image = [UIImage imageNamed:@"icon_0"];
    [self makeImageEditorWith: image];
}
//MARK: 去编辑图片
-(void)makeImageEditorWith:(UIImage *)image {
    StoryMakeImageEditorViewController * vc = [[StoryMakeImageEditorViewController alloc] initWithImage: image];
    vc.saveComplate = ^{
        NSLog(@"保存完成 请刷新数据");
        [self.mViewModel reloadData];
    };
    [self customShow:vc];
}

//MARK: UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegateFlowLayout

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage * image = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:true completion:^{
        [self makeImageEditorWith:image];
    }];
}

//MARK: UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return [self.mViewModel sectionCount];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.mViewModel cellCount:section];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeCollectionViewCell identifierForReuseCell] forIndexPath:indexPath];
    NSURL * url = [self.mViewModel cellDataFor:indexPath];
    [cell setData:url];
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSURL * url = [self.mViewModel cellDataFor:indexPath];
    NSData * data = [NSData dataWithContentsOfURL:url];
    UIImage * dataImg = [[UIImage alloc] initWithData: data];
    [self makeImageEditorWith:dataImg];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (self.view.bounds.size.width - 5 * 4 - 1 ) / 3;
    CGSize size = CGSizeMake(width, width);
    return  size;
}

//MARK: 设置collection view的一些基础属性
-(void)configureCollectionView {
    
    UINib * nib = [UINib nibWithNibName:@"HomeCollectionViewCell" bundle:[NSBundle mainBundle]];
    [self.mCollectionView registerNib:nib forCellWithReuseIdentifier:[HomeCollectionViewCell identifierForReuseCell]];
    
    self.mCollectionView.showsVerticalScrollIndicator = false;
    self.mCollectionView.showsHorizontalScrollIndicator = false;
    
    self.mFlow.minimumLineSpacing = 5;
    self.mFlow.minimumInteritemSpacing = 5;
    self.mFlow.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    [self.view insertSubview:self.mCollectionView aboveSubview:self.topView];
    [self.mCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    self.mCollectionView.delegate = self;
    self.mCollectionView.dataSource = self;
    
    [self.addBt setTitle:@"" forState:UIControlStateNormal];
    [self.moreBt setTitle:@"" forState:UIControlStateNormal];
}

//MARK: 对view model的一些设置
-(void)configureViewModel {
    __weak typeof(self) weakSelf = self;
    
    self.mViewModel.reloadComplate = ^{
        [weakSelf.mCollectionView reloadData];
    };
    
    [self.mViewModel reloadData];
}

//MARK: 懒加载
-(HomeViewModel *)mViewModel {
    if (nil == _mViewModel) {
        _mViewModel = [HomeViewModel new];
    }
    return _mViewModel;
}

-(UICollectionViewFlowLayout *)mFlow {
    if (nil == _mFlow) {
        _mFlow = [[UICollectionViewFlowLayout alloc] init];
    }
    return _mFlow;
}

-(UICollectionView *)mCollectionView {
    if (nil == _mCollectionView) {
        _mCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.mFlow];
    }
    return _mCollectionView;
}

@end
